home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / read_x11_bitmap.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  135 lines

  1. ; $Id: read_x11_bitmap.pro,v 1.3 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1991-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. ;+
  7. ; NAME:
  8. ;    READ_X11_BITMAP
  9. ;
  10. ; PURPOSE:
  11. ;    Read bitmaps stored in the X11 format.
  12. ;
  13. ;    The X Windows bitmap(1) program produces a C header file
  14. ;    containing the definition of a bitmap produced by that program.
  15. ;    This procedure reads such a file and creates an IDL byte array
  16. ;    containing the bitmap.
  17. ;
  18. ;    This procedure is used primarily to read bitmaps to be used as
  19. ;    IDL widget button labels.
  20. ;
  21. ; CATEGORY:
  22. ;    Bitmaps, X Windows, widgets.
  23. ;
  24. ; CALLING SEQUENCE:
  25. ;    READ_X11_BITMAP, File, Bitmap [, X, Y]
  26. ;
  27. ; INPUTS:
  28. ;    File: The name of the file containing the bitmap.
  29. ;
  30. ; KEYWORD PARAMETERS:
  31. ;
  32. ;    EXPAND_TO_BYTES: return a 2-D array which has one bit per byte
  33. ;        (0 for a 0 bit), (255 for a 1 bit) instead. (See example)
  34. ;
  35. ; OUTPUTS:
  36. ;    Bitmap: The variable in which to store the bitmap.  This variable
  37. ;        is returned as a byte array.
  38. ;
  39. ; OPTIONAL OUTPUT PARAMETERS:
  40. ;    X:    The width of the bitmap is returned in this variable.
  41. ;
  42. ;    Y:    The height of the bitmap is returned in this variable.
  43. ;
  44. ; COMMON BLOCKS:
  45. ;    None.
  46. ;
  47. ; EXAMPLE:
  48. ;    To open and read the X11 bitmap file named "my.x11" in the current 
  49. ;    directory, store the bitmap in the variable BITMAP1, and the width
  50. ;    and height in the variables X and Y, enter:
  51. ;
  52. ;        READ_X11_BITMAP, "my.x11", BITMAP1, X, Y
  53. ;
  54. ;    To display the new bitmap, enter:
  55. ;
  56. ;        READ_X11_BITMAP, "my.x11", Image, /EXPAND_TO_BYTES
  57. ;        TV, Image, /ORDER
  58. ;
  59. ; MODIFICATION HISTORY:
  60. ;    10 January 1991, AB
  61. ;    1 Apr, 1992, CF fixed bug with bitmaps larger than 32k bytes.
  62. ;    24 March 1993, JWG fixed EXPAND_TO_BYTES option
  63. ;-
  64. ;
  65.  
  66. pro READ_X11_BITMAP, FILE, BITMAP, X, Y, EXPAND_TO_BYTES=EXPAND
  67.  
  68. on_error, 2        ; On error, return to caller
  69.  
  70. expand    = keyword_set(expand)
  71.  
  72. ; This array, indexed by an ASCII character, returns the hex value associated
  73. ; with that character in the least significant 4 bits of the byte.
  74. lo_ascii=bytarr(256, /nozero)
  75. lo_ascii[(byte('0'))[0]:(byte('9'))[0]] = bindgen(10)
  76. lo_ascii[(byte('A'))[0]:(byte('F'))[0]] = bindgen(6)+10B
  77. lo_ascii[(byte('a'))[0]:(byte('f'))[0]] = bindgen(6)+10B
  78.  
  79. ; This array, indexed by an ASCII character, returns the hex value associated
  80. ; with that character in the most significant 4 bits of the byte.
  81. hi_ascii=bytarr(256, /nozero)
  82. hi_ascii[(byte('0'))[0]:(byte('9'))[0]] = bindgen(10)*16B
  83. hi_ascii[(byte('A'))[0]:(byte('F'))[0]] = (bindgen(6)+10)*16B
  84. hi_ascii[(byte('a'))[0]:(byte('f'))[0]] = (bindgen(6)+10)*16B
  85.  
  86.  
  87. openr, unit, file, /get_lun
  88.  
  89. ; The width
  90. line = ''
  91. while (((p=strpos(line, '_width'))) eq -1) do begin
  92.   if (eof(unit)) then message,'Not an X11 bitmap file: ' + FILE
  93.   readf, unit, line
  94. endwhile
  95. x = long(strmid(line, p+6, 32767))
  96.  
  97. ; The height
  98. readf, unit, line
  99. while (((p=strpos(line, '_height'))) eq -1) do readf, unit, line
  100. y = long(strmid(line, p+7, 32767))
  101.  
  102. ; The result array
  103.   x_bytes = (x + 7) / 8
  104.   BITMAP=bytarr(X_BYTES, Y, /nozero)
  105.  
  106. ; Get to the start of the data
  107. while (((p=strpos(line, '[]'))) eq -1) do readf, unit, line
  108.  
  109. ; Read it
  110. readf, unit, line
  111. pos=0
  112. for i = 0L, x_bytes * y - 1 do begin
  113.   pos = strpos(line, '0x', pos)
  114.   if (pos eq -1) then begin
  115.     readf, unit, line
  116.     pos = strpos(line, '0x')
  117.   endif
  118.   tmp = byte(strmid(line, pos+2, 2))
  119.   pos=pos+4
  120.   bitmap[i] = hi_ascii[tmp[0]] + lo_ascii[tmp[1]]
  121. endfor
  122.  
  123.   if (expand) then begin
  124.     tmp=bitmap
  125.     bitmap=bytarr(X, Y)
  126.     for i=0,X-1 do begin
  127.     byte = i / 8
  128.     mask = 2 ^ (i mod 8)
  129.     ind = where( tmp[byte,*] and mask, count)
  130.     if count ne 0 then bitmap[i, ind] = 255
  131.     endfor
  132.   endif
  133. free_lun, unit
  134. end
  135.